Variant Class

A Variant is a special data type that can contain any type of data. Use the VarType function or the Variant's Type method to determine the data type of a variant.

Events

None

Properties

BooleanValue

ColorValue

DateValue

DoubleValue

IntegerValue

ObjectValue

StringValue


Methods

Equals

Hash

IsNull

IsNumeric

Type


More information available in parent classes: Object


Notes

The default value of a Variant is 0. When you assign a variant to a String, numeric, or Date variable, REALbasic converts the value of the variant to the variable's data type. For example,

Dim v as Variant
Dim n as Integer
Dim s as String
v=25
s=v   // s is "25"
v="25"
n=v // n is 25

If there is any ambiguity concerning the type conversion of a variant, you should use one of the properties of the Variant class to force the REALbasic compiler to convert the variant to the desired type. In this example, the Product number is supposed to be the string concatenation of the Model number and the Part Number. To do this, use the StringValue property to tell REALbasic to do what you want:

Dim model,partNumber,product as Variant
Model=100
Partnumber=546
Product=Model+Partnumber //product=646
Product=Model.StringValue+Partnumber.StringValue  //product=100546

Or, you could just type the variables as String and do string concatenation directly.

The Type method returns the type of the variant, provided it is not Nil. For example, the following code generates an NilObjectException error

Dim v as Variant
Dim i as Integer
i=v.type

.

On the other hand, the following example returns 0.

Dim v as Variant
Dim i as Integer
i= VarType(v)

Variants used in an expression convert themselves to the type of the other operand, no matter what kind of data they contain, instead of deferring the type-conversion until runtime. This only affects expressions involving a variant operand and an operand of some other type, not expressions in which both operands are variants.

Consider the following example:

Dim v as Variant = 0.5

The comparison:

If v > 0 then

returns False since v is being treated as an Integer. If you instead use:

If v > 0.0 then..

the comparison returns True.

The comparison

If v.DoubleValue > 0

also returns True.


See Also

IsNumeric, VarType functions; DatabaseField, Date, Dictionary classes; Boolean, Color, Double, Integer, Single, String data types; Dim statement.